package com.c.yinyuezhushou.Adapter;

import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.c.yinyuezhushou.Dialog.MyDialog;
import com.c.yinyuezhushou.Dialog.SongListBoDialog;
import com.c.yinyuezhushou.Entity.SongListEntity;
import com.c.yinyuezhushou.Fragment.LoveHistoryFragment;
import com.c.yinyuezhushou.Fragment.MineFragment;
import com.c.yinyuezhushou.Interface.MyInterface;
import com.c.yinyuezhushou.MRetrofit.MusicItem;
import com.c.yinyuezhushou.MyApplication;
import com.c.yinyuezhushou.R;
import com.c.yinyuezhushou.Ui.ListActivity;
import java.util.List;

public class SongListAdapter extends RecyclerView.Adapter{
    private List<SongListEntity> listEntities;
    private MyApplication myApplication;
    private MineFragment mineFragment;

    public SongListAdapter(List<SongListEntity> list, MyApplication myApplication, MineFragment mineFragment) {
        listEntities = list;
        this.myApplication =myApplication;
        this.mineFragment = mineFragment;
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        TextView textView;
        ImageView imageView;
        ImageView imageView2;
        RelativeLayout relativeLayout;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.islr_image1);
            imageView2 = itemView.findViewById(R.id.islr_image2);
            textView = itemView.findViewById(R.id.islr_text);
            relativeLayout = itemView.findViewById(R.id.islr_re);
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_song_list_rec,parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.textView.setText(listEntities.get(position).getListname());

        if (listEntities.get(position).getImageurl().equals("")){
            Glide.with(viewHolder.imageView.getContext()).load(R.drawable.icon_songlist).apply(RequestOptions.bitmapTransform(new RoundedCorners(20))).into(viewHolder.imageView);
        }else{
            Glide.with(viewHolder.imageView.getContext()).load(listEntities.get(position).getImageurl()).apply(RequestOptions.bitmapTransform(new RoundedCorners(20))).into(viewHolder.imageView);
        }
        viewHolder.relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<MusicItem> musicItems = mineFragment.getMyDButil().getSongListMusicItem(viewHolder.textView.getText().toString());
                myApplication.setFragment(new LoveHistoryFragment(musicItems, new MyInterface() {
                    @Override
                    public void con() {

                    }

                    @Override
                    public void can() {

                    }

                    @Override
                    public void musicItem(MusicItem musicItem) {
                        MyDialog myDialog = new MyDialog(myApplication.getFragment().getContext(), true, null, "是否移除？","", new MyInterface() {
                            @Override
                            public void con() {
                                if (myApplication.getMyDButil().removeMusicItemTable(musicItem,"SongListMusicItem")){
                                    Toast.makeText(myApplication, "移除成功", Toast.LENGTH_SHORT).show();
                                }else {
                                    Toast.makeText(myApplication, "移除失败", Toast.LENGTH_SHORT).show();
                                }
                            }

                            @Override
                            public void can() {

                            }

                            @Override
                            public void musicItem(MusicItem musicItem) {

                            }
                        });
                        myDialog.show();
                    }
                }));
                Intent intent = new Intent(view.getContext(), ListActivity.class);
                intent.putExtra("title",viewHolder.textView.getText().toString());
                view.getContext().startActivity(intent);
            }
        });
        viewHolder.imageView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SongListBoDialog songListBoDialog = new SongListBoDialog(listEntities.get(position),mineFragment);
                songListBoDialog.show(mineFragment.getFragmentManager(),null);
            }
        });
    }

    @Override
    public int getItemCount() {
        return listEntities.size();
    }
}
